package com.uroad.lib.widget.loadmore;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.LayoutAnimationController;
import android.widget.AbsListView;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.TextView;

import com.uroad.lib.R;

/**
 * used to loadmore
 * Created by jiadong on 2016/11/9.
 */
public class LoadMoreListView extends ListView implements AbsListView.OnScrollListener {

    private int visibleItemCount;
    private int visibleLastIndex;
    private boolean isLoading, canLoadMore;

    // footer view
    private LayoutInflater mInflater;
    private LinearLayout mFooterView;
    private ProgressBar load_more_progressBar;
    private TextView load_more_textView;

    private Context context;
    private OnLoadMoreListener mOnLoadMoreListener;
    private OtherActionListener otherActionListener;

    public LoadMoreListView(Context context) {
        super(context);
        init(context);
    }

    public LoadMoreListView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    public LoadMoreListView(Context context, AttributeSet attrs, int defStyle) {
        super(context, attrs, defStyle);
        init(context);
    }

    private void init(Context context) {
        this.context = context;
        mInflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        // footer
        mFooterView = (LinearLayout) mInflater.inflate(
                R.layout.loadmore_footer, this, false);
        load_more_progressBar = (ProgressBar) mFooterView.findViewById(R.id.load_more_progressBar);
        load_more_textView = (TextView) mFooterView.findViewById(R.id.load_more_textView);
        addFooterView(mFooterView, null, false);
        this.setOnScrollListener(this);
    }

    public void startAnimation(Context context) {
        Animation animation = AnimationUtils.loadAnimation(context, R.anim.listview_alpha_right_in);
        LayoutAnimationController controller = new LayoutAnimationController(animation);
        controller.setDelay(0.5f);
        controller.setOrder(LayoutAnimationController.ORDER_NORMAL);
        setLayoutAnimation(controller);
    }

    @Override
    public void setAdapter(ListAdapter adapter) {
        super.setAdapter(adapter);
    }

    @Override
    public void onScrollStateChanged(AbsListView view, int scrollState) {
        int itemLastIndex = getAdapter().getCount() - 1;
        if (scrollState == OnScrollListener.SCROLL_STATE_IDLE && visibleLastIndex == itemLastIndex) {
            if (!isLoading && canLoadMore) {
                isLoading = true;
                mOnLoadMoreListener.onLoadMore();
                mFooterView.setVisibility(View.VISIBLE);
                load_more_progressBar.setVisibility(VISIBLE);
                load_more_textView.setText("正在加载...");
                load_more_textView.setTextColor(context.getResources().getColor(R.color.darkgray));
            }
        }
        if (otherActionListener != null) {
            otherActionListener.doOtherAction(scrollState);
        }
    }

    @Override
    public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
        this.visibleItemCount = visibleItemCount;
        visibleLastIndex = firstVisibleItem + visibleItemCount - 1;
    }

    public void setLoadMoreListener(OnLoadMoreListener moreListener) {
        this.mOnLoadMoreListener = moreListener;
    }

    public void setOtherActionListener(OtherActionListener otherActionListener) {
        this.otherActionListener = otherActionListener;
    }

    public void startLoading() {
        isLoading = true;
        mFooterView.setVisibility(View.VISIBLE);
        load_more_progressBar.setVisibility(VISIBLE);
        load_more_textView.setText("正在加载...");
        load_more_textView.setTextColor(context.getResources().getColor(R.color.darkgray));
    }

    public void setLoadComplete() {
        // this.setSelection(visibleLastIndex - visibleItemCount + 1);
        isLoading = false;
    }

    public void setCanLoadMore(boolean canLoadMore) {
        this.canLoadMore = canLoadMore;
        if (!canLoadMore) {
            mFooterView.setVisibility(View.VISIBLE);
            load_more_progressBar.setVisibility(GONE);
            load_more_textView.setTextColor(context.getResources().getColor(R.color.gray));
//            load_more_textView.setText("暂无更多数据");
            load_more_textView.setVisibility(View.GONE);
        }
    }

    public interface OnLoadMoreListener {
        void onLoadMore();

    }

    public interface OtherActionListener {
        void doOtherAction(int scrollStateIdle);
    }

    //隐藏底部“加载更多、暂无更多数据”文字
    public void hideLoadMoreText(){
        load_more_textView.setVisibility(View.GONE);
    }
}
